# -*- coding: UTF-8 -*-
import os
import smtplib
import sys
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


def send_mail(message):
    # 发送邮箱
    # sender='xxxxx@163.com'
    sender = '756686600@qq.com'
    # 接收邮箱
    receiver = '995595198@qq.com'
    # 发送邮箱服务器
    # smtpserver='smtp.163.com'
    smtpserver = 'smtp.qq.com'
    # 用户名  口令
    # username='18706710668@163.com'
    username = '756686600@qq.com'
    password = 'xxxx'

    # 中文需参数‘utf8’，单字节字符不需要
    # 发送邮件主题
    subject = '日志监控报警'
    msg = MIMEMultipart('mixed')
    msg['Subject'] = Header(subject, 'utf-8')

    # 邮件正文
    text = message
    zw = MIMEText(text, 'plain', 'utf-8')
    msg.attach(zw)

    msg['to'] = '995595198@qq.com'
    msg['from'] = '756686600@qq.com'

    smtp = smtplib.SMTP_SSL('smtp.qq.com', port=465)
    # smtp=smtplib.SMTP()
    # smtp.connect('smtp.qq.com')
    # smtp.set_debuglevel(1)
    smtp.login(username, password)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()


if __name__ == '__main__':
    arg = sys.argv[1]
    arg2 = sys.argv[2]
    msg = "Deal All\n  日志文件包含有：ERROR，时间发生在: " + arg + arg2
    send_mail(msg)
